package com.rede.didiok.user.modulelist.community.event.listener;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.TypeReference;
import com.rede.didiok.common.dto.user.UserDto;
import com.rede.didiok.common.enums.AuditStatusEnum;
import com.rede.didiok.common.exception.exceptionType.FeignException;
import com.rede.didiok.common.feign.UserFeignClient;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.common.utils.business.RabbitMqUtil;
import com.rede.didiok.user.modulelist.community.entity.MomentCommentEntity;
import com.rede.didiok.user.modulelist.community.event.momentCommentEvent.MomentCommentAddEvent;
import com.rede.didiok.user.modulelist.community.event.momentCommentEvent.MomentCommentAuditEvent;
import com.rede.didiok.user.modulelist.community.event.momentCommentEvent.MomentCommentDeleteEvent;
import com.rede.didiok.user.modulelist.community.event.momentCommentEvent.MomentCommentEvent;

@Component
@Slf4j
public class MomentCommentListener {

    /**
     * 用户服务
     */
    @Resource
    private UserFeignClient userFeignClient;

    /**
     * 简易的mq邮件封装工具类
     */
    @Resource
    private RabbitMqUtil rabbitMqUtil;

    @EventListener(value = {MomentCommentAuditEvent.class})
    public void audit(MomentCommentEvent momentCommentEvent) {
        MomentCommentEntity comment = (MomentCommentEntity) momentCommentEvent.getSource();

        /**
         * 发送邮件
         */
        if (StringUtils.isNotEmpty(comment.getCreateUserUid())) {
            R result = userFeignClient.getUserById(comment.getCreateUserUid());
            UserDto user;
            if(result.getCode()==0){
                user = result.getData(new TypeReference<UserDto>(){});
            }else {
                // 获取异常信息
                String msg = result.getData("msg",new TypeReference<String>(){});
                throw new FeignException("feign调用异常（根据用户id找用户）："+msg);
            }

            StringBuilder text = new StringBuilder();
            text.append(String.format("<p>可爱的博主%s,您的评论</p>", user.getNickname()));
            if (AuditStatusEnum.REJECT.equals(comment.getAuditStatus())) {
                text.append("审核未通过,审核原因为" + comment.getRejectReason());
                rabbitMqUtil.sendSimpleEmail(user.getEmail(), text.toString());
            } else if (AuditStatusEnum.AGREE.equals(comment.getAuditStatus())) {
                text.append("审核已通过");
                rabbitMqUtil.sendSimpleEmail(user.getEmail(), text.toString());
            } else {
                log.error("审核状态异常,稿件为{}", comment.getUid());
            }
        }
    }

    @EventListener(value = {MomentCommentAddEvent.class})
    public void add(MomentCommentEvent momentCommentEvent) {

    }

    @EventListener(value = {MomentCommentDeleteEvent.class})
    public void delete(MomentCommentEvent momentCommentEvent) {

    }
}
